package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户服务
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;


    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);
        List<Role> result = page.getResult();
        long total = page.getTotal();
        return new PageResult(total,result);
    }

    @Override
    public void add(Role role, Integer[] menuIds, Integer[] permissionIds) {
        //新增用户，操作t_role表
        roleDao.add(role);
        //设置角色和用户的关联关系，操作中间表
        Integer roleId = role.getId();
        this.setRoleAndMenuAndPermission(roleId,menuIds,permissionIds);
    }

    //设置关联关系
    public void setRoleAndMenuAndPermission(Integer roleId, Integer[] menuIds,Integer[] permissionIds) {
        if (menuIds!=null && menuIds.length>0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("menuId",menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
        if (permissionIds != null && permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    //编辑角色信息，同时需要关联菜单和权限
    public void edit(Role role, Integer[] menuIds, Integer[] permissionIds) {
        //修改角色基本信息，操作用户t_role表
        roleDao.edit(role);
        //清理当前角色关联的菜单，操作中间关系表t_role_menu表
        roleDao.deleteMenuByRoleId(role.getId());
        //清理当前角色关联的权限，操作中间关系表t_role_permission表
        roleDao.deletePermissionByRoleId(role.getId());
        //重新建立当前用户和菜单的关联关系
        Integer id = role.getId();
        this.setRoleAndMenuAndPermission(id,menuIds,permissionIds);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

    @Override
    public void delete(Integer id) {
        /*//查询此角色是否有关联菜单或者权限表
        long count1 = roleDao.findCountMentByRoleId(id);
        long count2 = roleDao.findPermissionCountByRoleId(id);
        if (count1>0 || count2>0){
            throw new RuntimeException("当前权限被引用,不能删除");
        }
        roleDao.delete(id);*/


        roleDao.deleteMenuByRoleId(id);
        roleDao.deletePermissionByRoleId(id);
        roleDao.delete(id);
    }
}
